using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{

    public class CLSMAEGRP : ObservableObject
    {

MDOgeneral mod = new MDOgeneral();

#region Metodos basicos tabla
    public List<CLSMAEGRP> listaGrupos()
    {
        try
        {
            string query = "select CODGRP_GRP,NOMGRP_GRP,ACEDEVPRD_GRP from MAEGRP order by NOMGRP_GRP";
                DataTable val = mod.consulta(query);
                List<CLSMAEGRP> lisGrupos = new List<CLSMAEGRP>();
                foreach (DataRow r in val.Rows)
                {
                    lisGrupos.Add(new CLSMAEGRP { CODGRP_GRP = r["CODGRP_GRP"].ToString().Trim(), NOMGRP_GRP = r["NOMGRP_GRP"].ToString().Trim() });
                }
                return lisGrupos;
        }
        catch (Exception e) { throw e; }
    }
    public int actualizar()
    {
        try
        {
            string query = "update MAEGRP set NOMGRP_GRP='" + NOMGRP_GRP + "', ACEDEVPRD_GRP='" + ACEDEVPRD_GRP + "' where (CODGRP_GRP='" + CODGRP_GRP + "')";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }
    public int insertar()
    {
        try
        {
            string query = "insert into MAEGRP (CODGRP_GRP,NOMGRP_GRP,ACEDEVPRD_GRP) values ('" + CODGRP_GRP + "','" + NOMGRP_GRP + "','" + ACEDEVPRD_GRP + "')";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }
    public void consultar() 
    {
        try
        {
            string query = "select CODGRP_GRP,NOMGRP_GRP,ACEDEVPRD_GRP from MAEGRP where CODGRP_GRP='" + CODGRP_GRP + "'";
            DataTable val = mod.consulta(query);
            if (val.Rows.Count > 0)
            {
                this.NOMGRP_GRP = val.Rows[0]["NOMGRP_GRP"].ToString();
                this.ACEDEVPRD_GRP = val.Rows[0]["ACEDEVPRD_GRP"].ToString();
                EXISTE = true;
                CAMBIO = false;
            }
            else
            {
                 EXISTE = false;
            }
        }
        catch (Exception e) { throw e; }
    }
    public int eliminar()
    {
        try
        {
            if (!exiMov())
            {
                string query = "delete from MAEGRP where CODGRP_GRP='" + CODGRP_GRP + "'";
                return mod.comando(query);
            }
            return 0;
        }
        catch (Exception e) { throw e; }
    }
    public DataTable buscar()
    {
        try
        {
            string query = "select CODGRP_GRP as Codigo,NOMGRP_GRP as Nombre from MAEGRP where ";


            if (NOMGRP_GRP != "")
            {
                query = query + "UPPER(NOMGRP_GRP) like '%" + NOMGRP_GRP.ToUpper() + "%' and ";
            }

            query = query.Remove(query.Length - 4).Trim() + " Order by NOMGRP_GRP asc";

            return mod.consulta(query);
        }
        catch (Exception e) { throw e; }
    }
    public Boolean exiMov()
    {
        try
        {
            //Maestro materiales
            string query = "select count(*) from MAEMAT where CODGRP_MAT='" + CODGRP_GRP + "'";
            if (!mod.consulta(query).Rows[0][0].ToString().Equals("0"))
            {
                return true;
            }
            return false;
        }
        catch (Exception ex) { throw (ex); }
    }
#endregion

#region  Propiedades tabla MAEGRP
    private string _CODGRP_GRP;
    public string CODGRP_GRP
    {
        get 
        {
            try
            {
                return _CODGRP_GRP;
            }
            catch 
            {
                return null;
            }
        }
        set
        {   _CODGRP_GRP = value;
        RaisePropertyChangedEvent("CODGRP_GRP");
        }
    }
    private string _NOMGRP_GRP;
    public string NOMGRP_GRP{
        get 
        {
            try
            {
                return _NOMGRP_GRP;       
            }
            catch 
            {
                return null;
            }

        }
        set {
            if (_NOMGRP_GRP != value) { CAMBIO = true; }
            _NOMGRP_GRP = value;
            RaisePropertyChangedEvent("NOMGRP_GRP");
        }
    }
    private Boolean _CAMBIO=false;
    public Boolean CAMBIO
    {
        get { return _CAMBIO; }
        set
        {
            _CAMBIO = value;
            RaisePropertyChangedEvent("CAMBIO");
        }
    }
    private Boolean _EXISTE=false;
    public Boolean EXISTE
    {
        get { return _EXISTE; }
        set
        {
            _EXISTE = value;
            RaisePropertyChangedEvent("EXISTE");
        }
    }

    private string _ACEDEVPRD_GRP="N";
    public string ACEDEVPRD_GRP
    {
        get { return _ACEDEVPRD_GRP; }
        set
        {
            if (_ACEDEVPRD_GRP != value) { CAMBIO = true; }
            _ACEDEVPRD_GRP = value;
            RaisePropertyChangedEvent("ACEDEVPRD_GRP");
        }
    }


#endregion

      }
}
